using System;
using System.Collections.Concurrent;

namespace Sessionizer.Core.Unity
{
    public sealed class SessionStorage : ISessionStorage
    {
        private readonly ConcurrentDictionary<object, object> _registeredObjects;

        public SessionStorage()
        {
            _registeredObjects = new ConcurrentDictionary<object, object>();
        }

        public void Dispose()
        {
            _registeredObjects.Clear();
        }

        public Object Get(Object key)
        {
            Object instance;
            _registeredObjects.TryGetValue(key, out instance);
            return instance;
        }

        public void Set(Object key, Object instance)
        {
            _registeredObjects.AddOrUpdate(key, instance, (k, v) => instance);
        }
    }
}